﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Threading;
using System.Net.Sockets;

namespace jnxceq
{
    public class ClientHandle
    {
        private string m_returnData, m_sendData;//接收和发送数据

        private TcpClient m_clientsocket = null;//TcpClient实例

        byte[] data;//中间变量

        public TcpClient ClientSocket
        {
            get { return m_clientsocket; }
            set { m_clientsocket = value; }
        }

        //将数据转化为ＡＳＣＩＩ编码
        private byte[] EncodingASCII(string buf)
        {
            byte[] data = Encoding.ASCII.GetBytes(buf);
            return data;
        }

        public void ResponseClient()
        {
            if (m_clientsocket != null)
            {
                StreamReader rs = new StreamReader(m_clientsocket.GetStream());
                NetworkStream ws = m_clientsocket.GetStream();

                m_sendData = "resp";

                //data = EncodingASCII(m_sendData);//回传数据
                //ws.Write(data, 0, data.Length);

                while (true)
                {
                    m_returnData = rs.ReadLine();

                    if (m_returnData == "req")//接收心跳包
                    {
                        data = EncodingASCII(m_sendData);//回传数据
                        ws.Write(data, 0, data.Length);

                        Console.WriteLine("req");
                    }

                }
            }

            m_clientsocket.Close();

        }
    }
}
